﻿
Imports System.Threading
Imports System.Linq
Imports System.Runtime.InteropServices

Public Class Notepad
    Private notePadFile As NotePadFile = New NotePadFile()
    Private notePadEdit As NotePadEdit = New NotePadEdit()
    Private notepadspecking As New NotePadSpeaking()

    ' Variable for volumn
    Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
    Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
    Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
    Private Const WM_APPCOMMAND As Integer = &H319


    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        'notePadFile.OpenFile1(OpenFileDialog1)
        btnOpen1_Click(sender, e)
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        txtOpen.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        notepadspecking.StopPlay()
        p = False
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.Show()
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click

        btnOpen1_Click(sender, e)
        'npf.OpenFile1(OpenFileDialog1)
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        notePadFile.SaveFile(SaveFileDialog1, txtBody)
    End Sub

    Private Sub bteRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bteRefresh.Click
        notepadspecking.Reload()
    End Sub

    Private Sub btnExit1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit1.Click
        Me.Close()
    End Sub

    Private Sub btnOpen1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen1.Click
        Dim str As String = Nothing
        Dim c As Control
        str = notePadFile.OpenFile1(OpenFileDialog1)
        If str <> Nothing Then
            NewToolStripMenuItem_Click(sender, e)
            c = TabControl1.SelectedTab.Controls.Item(0)
            c.Text = str
        End If

    End Sub

    Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click
        notePadFile.SaveFile(SaveFileDialog1, txtBody)
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        notePadFile.SaveFile(SaveFileDialog1, txtBody)
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        NewToolStripMenuItem_Click(sender, e)
        'npf.AddNewFile(txtBody)
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click

        NotePadData.Instance().tabID += 1
        TabControl1.TabPages.Insert(TabControl1.TabPages.Count - 1, "Tabpage" & NotePadData.Instance().tabID)
        TabControl1.SelectTab(TabControl1.TabCount - 2)
        Dim txtBody_ As New RichTextBox
        txtBody_.Name = TabControl1.TabCount.ToString()
        TabControl1.SelectedTab.Controls.Add(txtBody_)
        txtBody_.Dock = DockStyle.Fill
        notePadFile.AddNewFile(txtBody_)
    End Sub

    Private Sub btnColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnColor.Click
        Dim dlg As New ColorDialog
        Dim c As Control
        Dim r As RichTextBox
        c = TabControl1.SelectedTab.Controls.Item(0)
        If dlg.ShowDialog = Windows.Forms.DialogResult.OK Then
            r = CType(c, RichTextBox)
            r.ForeColor = dlg.Color
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim dlg As New PrintDialog
        dlg.ShowDialog()
    End Sub

    Private Sub btnFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFont.Click
        Dim font As New FontDialog
        Dim c As Control
        Dim r As RichTextBox
        c = TabControl1.SelectedTab.Controls.Item(0)
        If font.ShowDialog = Windows.Forms.DialogResult.OK Then
            r = CType(c, RichTextBox)
            r.Font = font.Font
        End If
    End Sub

    Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fontToolStripMenuItem.Click
        Dim font As New FontDialog
        Dim c As Control
        Dim r As RichTextBox
        c = TabControl1.SelectedTab.Controls.Item(0)
        If font.ShowDialog = Windows.Forms.DialogResult.OK Then
            r = CType(c, RichTextBox)
            r.Font = font.Font
        End If
    End Sub

    Private Sub UndoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UndoToolStripMenuItem.Click
        notePadEdit.myUndo(txtBody)
    End Sub

    Private Sub DeleteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
        notePadFile.AddNewFile(txtBody)
    End Sub

    Private Sub CutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutToolStripMenuItem.Click
        notePadEdit.myCut(txtBody)
    End Sub

    Private Sub PastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PastToolStripMenuItem.Click
        notePadEdit.myPast(txtBody)
    End Sub

    Private Sub RedoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedoToolStripMenuItem.Click
        notePadEdit.myRedo(txtBody)
    End Sub

    Private Sub Notepad_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Form.Show()
        Thread.Sleep(5000)
        Me.SendToBack()
        Load_Form.Close()
        EnableOrDisableControls()
        volumnLabel.Hide()
        volumnScroll.Visible = False
    End Sub

    Dim c As Control
    Dim p As Boolean = True
    Private Sub btnReadPause_Click(sender As Object, e As EventArgs) Handles btnReadPause.Click

        c = TabControl1.SelectedTab.Controls.Item(0)
        Try
            If p = True Then
                notepadspecking.Play(c.Text)
                p = False
            Else
                notepadspecking.Reload()
                p = True
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

    Private Sub TabControl1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.Click

        If CType(sender, TabControl).SelectedIndex <> CType(sender, TabControl).TabCount - 1 Then
            Return
        End If

        NotePadData.Instance().tabID += 1
        TabControl1.TabPages.Insert(TabControl1.TabPages.Count - 1, "Tabpage" & NotePadData.Instance().tabID)
        TabControl1.SelectTab(TabControl1.TabCount - 2)
        Dim txtBody_ As New RichTextBox
        txtBody_.Name = TabControl1.TabCount.ToString()
        TabControl1.SelectedTab.Controls.Add(txtBody_)
        txtBody_.Dock = DockStyle.Fill
        notePadFile.AddNewFile(txtBody_)
    End Sub


    Dim s As Integer = 0 'variable for logic changing bgimage of sound volumn
    Private Sub btnVolume_Click(sender As Object, e As EventArgs) Handles btnVolume.Click

        If (s = 0) Then
            btnVolume.BackgroundImage = My.Resources.mutebtn

            volumnLabel.Hide()
            volumnScroll.Visible = False
            s = 1
        Else
            btnVolume.BackgroundImage = My.Resources.buttons_2

            volumnLabel.Show()
            volumnScroll.Visible = True
            s = 0
        End If
        SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
    End Sub

    ' Showing message when changing the volumn
    <DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
    Dim HScroll3 As Integer = 0
    Dim Count As Integer = 0


    Private Sub volumnScroll_Scroll(sender As Object, e As ScrollEventArgs) Handles volumnScroll.Scroll
        If Count = 1 Then
            If volumnScroll.Value > HScroll3 Then
                SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
            End If
            If volumnScroll.Value < HScroll3 Then
                SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
            End If
        End If
        HScroll3 = volumnScroll.Value
        Count = 1
    End Sub


    Private Sub btnUndo_Click(sender As Object, e As EventArgs) Handles btnUndo.Click
        txtBody.Undo()
        txtBody.Select()
        EnableOrDisableControls()
    End Sub


    Private Sub btnRedo_Click(sender As Object, e As EventArgs) Handles btnRedo.Click
        txtBody.Redo()
        txtBody.Select()
        EnableOrDisableControls()
    End Sub

    Dim si As Integer = 0

    Private Sub showicontop_Click(sender As Object, e As EventArgs) Handles showicontop.Click
        If si = 0 Then
            GroupBox1.Hide()
            showicontop.Text = "បង្ហាញអាយខុន"
            Me.TabControl1.Location = New Point(0, 40)
            Me.TabControl1.Size = New Size(990, 567)
            si = 1
        Else
            GroupBox1.Show()
            showicontop.Text = "បិទ​អាយខុន"
            Me.TabControl1.Size = New Size(990, 481)
            Me.TabControl1.Location = New Point(0, 125)

            si = 0
        End If
    End Sub

    Private Sub txtBody_TextChanged(sender As Object, e As EventArgs) Handles txtBody.TextChanged
        EnableOrDisableControls()
    End Sub

    Private Sub EnableOrDisableControls()

        btnUndo.Enabled = txtBody.CanUndo
        btnRedo.Enabled = txtBody.CanRedo

    End Sub


    Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
        Dim c As Control
        Dim rtb As RichTextBox

        c = TabControl1.SelectedTab.Controls.Item(0)
        rtb = CType(c, RichTextBox)
        Dim startindex As Integer = 0
        Dim Searchstart As Integer = 0

        If txtSearch.Text.Length > 0 Then
            startindex = notePadFile.FindMyText(txtSearch.Text.Trim(), Searchstart, rtb.Text.Length, rtb)
        End If

        rtb.SelectionColor = Color.Black
        ' If string was found in the RichTextBox, highlight it
        If startindex >= 0 Then
            ' Set the highlight color as red
            rtb.SelectionColor = Color.Red
            ' Find the end index. End Index = number of characters in textbox
            Dim endindex As Integer = txtSearch.Text.Length
            ' Highlight the search string
            rtb.Select(startindex, endindex)
            ' mark the start position after the position of
            ' last search string
            Searchstart = startindex + endindex
        End If
        txtSearch.Text = ""
    End Sub
End Class
